package com.thwj.project.biz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thwj.common.exception.ServiceException;
import com.thwj.project.app.service.AppActivityService;
import com.thwj.project.biz.domain.Activity;
import com.thwj.project.biz.domain.ActivitySignup;
import com.thwj.project.biz.enums.ActivitySignUpStatusEnum;
import com.thwj.project.biz.mapper.ActivitySignupMapper;
import com.thwj.project.biz.service.IActivityService;
import com.thwj.project.biz.service.IActivitySignupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 活动报名Service业务层处理
 *
 * @author generate
 * @date 2023-06-05
 */
@Service
public class ActivitySignupServiceImpl extends ServiceImpl<ActivitySignupMapper, ActivitySignup> implements IActivitySignupService {

    @Autowired
    AppActivityService appActivityService;
    @Autowired
    IActivityService activityService;

    @Transactional
    @Override
    public int cancelSignUp(ActivitySignup activitySignup) {
        // 读取当前报名信息
        ActivitySignup current = this.getById(activitySignup.getId());
        if(current == null || ActivitySignUpStatusEnum.sign_up_ed.getValue() != current.getStatus()) {
            throw new ServiceException("数据状态不合法");
        }
        //
        if(StringUtils.isEmpty(activitySignup.getCancelReason())) {
            throw new ServiceException("取消原因必填，exp: 经客户反馈同意取消");
        }
        if (ActivitySignUpStatusEnum.cancel_sign_up.getValue() != activitySignup.getStatus()) {
            throw new ServiceException("请选择 状态为 取消报名");
        }
        // 开始处理
        this.updateById(activitySignup);
        Activity activity = activityService.getById(activitySignup.getActivityId());
        // 判断是否存在退款 需要处理
        if (StringUtils.isNotEmpty(current.getOrderNumber())) {
            appActivityService.singleActivityRefund(current, activity);
        }
        return 0;
    }
}
